package com.example.jjc.rxjava_retrofit2_demo.module;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.jjc.rxjava_retrofit2_demo.BaseFragment;
import com.example.jjc.rxjava_retrofit2_demo.R;
import com.example.jjc.rxjava_retrofit2_demo.adapter.ItemListAdapter;
import com.example.jjc.rxjava_retrofit2_demo.model.Item;
import com.example.jjc.rxjava_retrofit2_demo.network.Api;
import com.example.jjc.rxjava_retrofit2_demo.util.GankBeautyResultToItemsMapper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 第二个页面
 * Created by Administrator on 2017/3/15.
 */

public class Fra_2 extends BaseFragment {
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.btn_pre_pager)
    ImageView btnPrePager;
    @BindView(R.id.btn_next_pager)
    ImageView btnNextPager;

    private int page = 1;//当前页码
    private final int COUNT = 10;//每一页加载的图片数量
    ItemListAdapter adapter = new ItemListAdapter();
    //本页面观察者
    Observer<List<Item>> observer = new Observer<List<Item>>() {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            swipeRefreshLayout.setRefreshing(false);
            showToast("数据加载失败");
        }

        @Override
        public void onNext(final List<Item> images) {//成功加载数据
            swipeRefreshLayout.setRefreshing(false);
            adapter.setItems(images);
            adapter.setOnItemClickListener(new ItemListAdapter.OnItemClick() {
                @Override
                public void onItemClick(View view) {
                    Dialog dialog = new Dialog(getActivity(),R.style.style_dialog);
                    View imageLayout = View.inflate(getActivity(),R.layout.show_image,null);
                    ImageView image = (ImageView) imageLayout.findViewById(R.id.iv_image);
                    String image_url = images.get(rvImage.getChildAdapterPosition(view)).image_url;
                    Glide.with(image.getContext()).load(image_url).into(image);
                    Display display = getActivity().getWindowManager().getDefaultDisplay();
                    int width = display.getWidth();
                    int height = display.getHeight();
                    dialog.setContentView(imageLayout,new ViewGroup.LayoutParams(width, height/2));
                    dialog.show();
                }
            });
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fra_2, container, false);
        ButterKnife.bind(this, view);

        initView();
        loadPager(page);
        return view;
    }

    private void initView() {
        rvImage.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        rvImage.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light
                ,android.R.color.holo_blue_light
                ,android.R.color.holo_green_light
                ,android.R.color.holo_purple);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadPager(page);
            }
        });
    }


    @OnClick({R.id.btn_pre_pager, R.id.btn_next_pager})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pre_pager://上一页
                if (page == 1){
                    return;
                }
                page--;
                loadPager(page);
                if (page == 1){
                    btnPrePager.setEnabled(false);
                    btnPrePager.setImageResource(R.drawable.nextpage_disable);
                }
                break;
            case R.id.btn_next_pager://下一页
                page++;
                loadPager(page);
                Log.d("page",page + "");
                if (page == 2){
                    btnPrePager.setEnabled(true);
                    btnPrePager.setImageResource(R.drawable.nextpage);
                }
                break;
        }
    }

    /**
     * 根据页码请求对应数据
     * @param page
     */
    private void loadPager(int page){
        swipeRefreshLayout.setRefreshing(true);
        unSubscription();
        //订阅
        subscription = Api.getGankApi()
                .getBeauties(COUNT,page)
                .map(GankBeautyResultToItemsMapper.getINSTANCE())
                .subscribeOn(Schedulers.io())//订阅运行在io线程上
                .observeOn(AndroidSchedulers.mainThread())//观察者运行在主线程上
                .subscribe(observer);
    }
}
